package com.zhangfuyin.aop;

import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class ProxyService implements InvocationHandler {

    private Object target;

    public <T> T getProxyInstance(T target){
        this.target = target;
        target =  (T)Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),this );

         return target;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        System.out.println("===================开启事务===================");
        Object rtn =  method.invoke(target);
        System.out.println("===================提交事务===================");
        return  rtn;
    }


//    @Override
//    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        System.out.println("===================开启事务===================");
//        Object rtn =  method.invoke(args);
//        System.out.println("===================提交事务===================");
//        return  rtn;
//    }
}
